package cc.lj.internal.coupons.infrastructure.repository.mysql;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.BasePageHelper;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketBatchRepository;
import cc.lj.internal.coupons.infrastructure.dao.ITicketBatchMapper;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.CapBatchPO;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchPO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 电子券批次表　mysql仓储实现
 * @author liangxifeng
 * @date 2021-12-14
 */
@Repository("ticketBatchMysqlRespository")
public class TicketBatchMysqlRespository implements ITicketBatchRepository,BasePageHelper<TicketBatchPO> {
    @Autowired
    private ITicketBatchMapper mapper;
    @Override
    public List list(TicketBatchPO entityPO) {
        QueryWrapper<TicketBatchPO> wrapper = new QueryWrapper<>(entityPO);
        List<TicketBatchPO> entitys = mapper.selectList(wrapper);
        if(entitys == null)
        {
            throw new ServiceException(CoupousExceptionEnum.TICKET_BATCH_LIST_NULL);
        }
        return entitys;
    }

    @Override
    public Integer updateById(TicketBatchPO entityPO) {
        Integer res = mapper.updateById(entityPO);
        if (res != 1)
        {
            throw new ServiceException(CoupousExceptionEnum.UPDATE_TICKET_BATCH_FAILED);
        }
        return res;
    }

    @Override
    public TicketBatchPO getById(Integer id) {
        TicketBatchPO entityPO =  mapper.selectById(id);
        if(entityPO==null) {
            throw new ServiceException(CoupousExceptionEnum.TICKET_BATCH_NOT_FOUND);
        }
        return entityPO;
    }

    @Override
    public PageInfo<TicketBatchPO> selectPage(PageParam pageParam) {
        return this.page(pageParam);
    }


    @Override
    public List<TicketBatchPO> getList(TicketBatchPO ticketBatchPO) {
        return this.list(ticketBatchPO);
    }

    @Override
    public List<TicketBatchPO> selectBatchIds(List<Integer> batchIdList) {
        return mapper.selectBatchIds(batchIdList);
    }

    @Override
    public List<TicketBatchPO> getNoBindCodeListByIds(List<Integer> batchIdList) {
        LambdaQueryWrapper<TicketBatchPO> wrapper = new LambdaQueryWrapper();
        wrapper.eq(TicketBatchPO::getActivityCode, ""); //活动编码=""
        wrapper.gt(TicketBatchPO::getBatchStoreShare, 0); //卖场分摊>0
        wrapper.in(TicketBatchPO::getBatchId,batchIdList); //主键id IN
        List<TicketBatchPO> entitys = mapper.selectList(wrapper);
        return entitys;
    }

    @Override
    public List<TicketBatchPO> getByIds(List<Integer> batchIds) {
        return mapper.selectBatchIds(batchIds);
    }
}
